package com.lemonfish.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lemonfish.enumcode.CodeEnum;
import com.lemonfish.pojo.entity.Goods;
import com.lemonfish.pojo.vo.GoodsVO;
import com.lemonfish.service.GoodsService;
import com.lemonfish.util.MyJsonResult;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商品 相关Controller
 *
 * @author Lemonfish
 * @version 1.0
 * @date 2020-03-11
 * Copyright © Chao Zhang
 */
@Api(tags = { "商品" })
@RestController
@RequestMapping("/goods")
@Slf4j
public class GoodsController extends BaseCrudMappingRestController<Goods, GoodsVO> {

    @Autowired
    private GoodsService goodsService;

    /**
     * 获取页面数据
     *
     * @param curPage 当前页码
     * @param size    每页条数
     * @param order   排序
     * @param column  排序的字段
     * @return
     */
    @GetMapping("/{curPage}/{size}")
    public MyJsonResult getUserList(@PathVariable("curPage") long curPage, @PathVariable("size") long size, @RequestParam("order") String order, @RequestParam("orderCol") String column) {
        IPage<Goods> goodsList = goodsService.getGoodsList(curPage, size, order, column);
        if (goodsList != null) {
            return MyJsonResult.success(goodsList);
        } else {
            return MyJsonResult.fail(CodeEnum.FAIL_NOT_FOUND);
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public MyJsonResult delete(@PathVariable("id") int id) {
        boolean result = goodsService.deleteEntity(id);
        if (result) {
            return MyJsonResult.success(null);
        } else {
            return MyJsonResult.fail(CodeEnum.FAIL_OPERATION);
        }
    }
}
